package com.longrise.android.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo.State;

public class IntenetUtil 
{
	public static final int NETWORN_NONE = 0;//无网络
	public static final int NETWORN_WIFI = 1;//Wife
	public static final int NETWORN_MOBILE = 2;//手机网络

	public static int getNetworkState(Context context) 
	{
		if(context == null)
		{
			return NETWORN_NONE;
		}
		//网络管理对象
		ConnectivityManager connManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		if(null == connManager)
		{
			return NETWORN_NONE;
		}
		// Wifi
		State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
				.getState();
		if (state == State.CONNECTED || state == State.CONNECTING) 
		{
			return NETWORN_WIFI;
		}

		// 3G
		if(null == connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE))
		{
			return NETWORN_NONE;
		}
		state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
		if (state == State.CONNECTED || state == State.CONNECTING)
		{
			return NETWORN_MOBILE;				
		}
		return NETWORN_NONE;
	}

}
